package com.gpnu7_412.yanzhi.controller.user;

import com.github.pagehelper.PageInfo;
import com.gpnu7_412.yanzhi.dto.ConciseUserDto;
import com.gpnu7_412.yanzhi.dto.UserMessageDto;
import com.gpnu7_412.yanzhi.exception.custom.PermissionDeniedException;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.service.InterestService;
import com.gpnu7_412.yanzhi.service.UserService;
import com.gpnu7_412.yanzhi.util.Result;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HJJ
 * @author ZZH
 */
@CrossOrigin
@RestController
@RequestMapping("/user/select")
public class SelectController {

    @Autowired
    private UserService userService;
    @Autowired
    private InterestService interestService;

    /**
     * 获取当前登录用户的全部信息
     * @return 响应数据
     * @deprecated
     */
    @GetMapping("/current")
    public Result getAllMessage() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        UserMessageDto userMessageDto = userService.getUserMessageById(userId);
        if (userMessageDto == null) {
            throw new RequestDataException("参数为空");
        }
        return new Result(userMessageDto);
    }

    /**
     * 根据Id获取用户详情
     * @param userId 用户ID
     * @return 用户详情对象
     */
    @GetMapping("/id")
    public Result getUserMessageById(@RequestParam("userId") Integer userId) {
        UserMessageDto userMessageDto = userService.getUserMessageById(userId);
        if (userMessageDto == null) {
            throw new RequestDataException("参数为空");
        }
        return new Result(userMessageDto);
    }

    /**
     * 分页查询所有用户简洁信息
     * @param pageNum 页数
     * @param pageSize 页面大小
     * @return 响应对象
     */
    @GetMapping("/all")
    public Result getAllUsers(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<ConciseUserDto> conciseUserDtoPageInfo = userService.getAllUsersByPage(pageNum, pageSize);
        return new Result(conciseUserDtoPageInfo);
    }

    /**
     * 根据用户名模糊查找用户
     * @param userName 模糊用户名
     * @return 简洁用户对象列表
     */
    @GetMapping("/name")
    public Result getConciseUserByName(@RequestParam("userName") String userName) {
        List<ConciseUserDto> conciseUsers = userService.getConciseUsersByName(userName);
        if (conciseUsers == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", conciseUsers.size());
        responseMap.put("list", conciseUsers);
        return new Result(responseMap);
    }

    /**
     * 根据用户邮箱前缀模糊查找用户
     * 该接口仅允许管理员调用
     * @param email 邮箱前缀
     * @return 简洁用户对象列表
     */
    @GetMapping("/email")
    public Result getUserByEmail(@RequestParam("email") String email) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String permission = (String) map.get("permission");
        if (!"管理员".equals(permission)) {
            throw new PermissionDeniedException("权限不足");
        }
        List<ConciseUserDto> conciseUsers = userService.getConciseUsersByEmail(email);
        if (conciseUsers == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", conciseUsers.size());
        responseMap.put("list", conciseUsers);
        return new Result(responseMap);
    }



    /**
     * 查看用户粉丝列表
     * @param userId 用户ID
     * @return 简洁用户对象列表
     */
    @GetMapping("/fans")
    public Result getUserFans(@RequestParam("userId") Integer userId) {
        List<ConciseUserDto> conciseUsers = interestService.getFans(userId);
        if (conciseUsers == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", conciseUsers.size());
        responseMap.put("list", conciseUsers);
        return new Result(responseMap);
    }

    /**
     * 查看用户关注列表
     * @param userId 用户ID
     * @return 简洁用户对象列表
     */
    @GetMapping("/like")
    public Result getUserLike(@RequestParam("userId") Integer userId) {
        List<ConciseUserDto> conciseUsers = interestService.getLike(userId);
        if (conciseUsers == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", conciseUsers.size());
        responseMap.put("list", conciseUsers);
        return new Result(responseMap);
    }

    /**
     * 判断是否关注
     * @param likeUserId 待查用户ID
     * @return 响应对象
     */
    @GetMapping("/like/status")
    public Result getUserIsLike(@RequestParam("likeUserId") Integer likeUserId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (interestService.isLike(userId, likeUserId)) {
            return new Result(true);
        }
        else {
            return new Result(false);
        }
    }
}
